package com.example.zhanghehe.myapplication

import android.content.Context
import android.opengl.GLSurfaceView
import android.view.MotionEvent

class MyGLSurfaceView(context:Context):GLSurfaceView(context){

    private var mRender: MyGLRender

    init {
        setEGLContextClientVersion(2)
        super.setEGLConfigChooser(8,8,8,8,16,0)
        mRender=MyGLRender()
        setRenderer(mRender)
        renderMode=GLSurfaceView.RENDERMODE_WHEN_DIRTY
    }

    private var mPreviousX: Float=0f

    private var mPreviousY: Float=0f

    override fun onTouchEvent(event: MotionEvent): Boolean {
        val x=event.x
        val y=event.y

        when(event.action){
            MotionEvent.ACTION_MOVE->{
                var dx=x-mPreviousX
                var dy=x-mPreviousY


                if(y>height/2){
                    dx=dx*-1
                }

                if(x<width/2){
                    dy=dy*-1
                }

                mRender.mAngle=mRender.mAngle+
                        (dx+dy)*TOUCH_SCALE_FACTOR
                requestRender()
            }
        }

        mPreviousX=x
        mPreviousY=y
        return true
    }

    companion object {
        private val TOUCH_SCALE_FACTOR=180.0f/320
    }

}